<?php
//require section
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . '/check_user_login.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_upload.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_movies.php';
$title = 'Admin Page';

function add($number) {
    $title_save = "";
    if (!empty($_POST['Title'.$number])) {
        $title_save = $_POST['Title'.$number];
    }
    $description_save = "";
    if (!empty($_POST['Description'.$number])) {
        $description_save = $_POST['Description'.$number];
    }
    $poster_save = "";
    if (!empty($_FILES['File_Poster'.$number]['size'])) {
        $lib_upload = new lib_upload();
        if ($lib_upload->upload_file('File_Poster'.$number, DOCUMENT_ROOT . DIR_SHARED_UPLOAD_IMAGES_POSTERS)) {
            $poster_save = $_FILES['File_Poster'.$number]['name'];
        }
    } else if (!empty($_POST['Poster'.$number])) {
        $poster_save = $_POST['Poster'.$number];
    }
    
    $status_save = api_movies::get_default_movie_statuses();
    if (!empty($_POST['Status'.$number])) {
        $status_save = $_POST['Status'.$number];
    }
    
    $error = api_movies::validate_movie_fields($title_save, $description_save, $poster_save);
    if (empty($error)) {
        $status_id = api_movies::get_status_id_from_name($status_save);
        if (api_movies::save_movie(0, $title_save, $status_id, $description_save, $poster_save)) {
            $success_info = "";
            $success_info .= '<div class="ui success message">';
            $success_info .= '<i class="close icon"></i>';
            $success_info .= '<div class="header">';
            $success_info .= "Added &lt;".$title_save."&gt; movies successfully.";
            $success_info .= "</div></div>";
            return $success_info;
        }
    }
    
    $error_show = '';
    $error_show .= '<div class="ui error message">';
    $error_show .= '    <i class="close icon"></i>';
    $error_show .= '    <div class="header">';
    $error_show .= '        add &lt;'.$title_save.'&gt; movies failed with these wrongs.';
    $error_show .= '    </div>';
    $error_show .= '    <ul class="list">';
    $error_show .= $error;
    $error_show .= '    </ul>';
    $error_show .= '</div>';
    return $error_show;
}


$id = 0;
$title = "";
$description = "";
$poster = "";
$status = api_movies::get_default_movie_statuses();
$action = "Add";

if (!empty($_GET['Action']) && $_GET['Action'] === 'Edit') {
    if (!empty($_GET['Id'])) {
        $id = $_GET['Id'];
    }
    $edit_item = api_movies::get_movie_by_id($id);
    $id = $edit_item['id'];
    $title = $edit_item['title'];
    $description = $edit_item['description'];
    $poster = $edit_item['poster'];
    $status = $edit_item['status_name'];
    $action = "Update";
} else if (!empty($_POST)) {
    $id_save = "";
    if (!empty($_POST['Id'])) {
        $id_save = $_POST['Id'];
    }
    $title_save = "";
    if (!empty($_POST['Title'])) {
        $title_save = $_POST['Title'];
    }
    $description_save = "";
    if (!empty($_POST['Description'])) {
        $description_save = $_POST['Description'];
    }
    $poster_save = "";
    if (!empty($_FILES['File_Poster']['size'])) {
        $lib_upload = new lib_upload();
        if ($lib_upload->upload_file('File_Poster', DOCUMENT_ROOT . DIR_SHARED_UPLOAD_IMAGES_POSTERS)) {
            $poster_save = $_FILES['File_Poster']['name'];
        }
    } else if (!empty($_POST['Poster'])) {
        $poster_save = $_POST['Poster'];
    }
    
    $status_save = "";
    if (!empty($_POST['Status'])) {
        $status_save = $_POST['Status'];
    }
    if (!empty($_POST['Action']) && $_POST['Action'] === "Update") {
        $error_show = api_movies::validate_movie_fields($title_save, $description_save, $poster_save);
        if (empty($error_show)) {
            $status_id = api_movies::get_status_id_from_name($status_save);
            if (api_movies::save_movie($id_save, $title_save, $status_id, $description_save, $poster_save)) {
                $success_info = "Update &lt;".$title_save."&gt; movie successfully.";
            }
        } else {
            $id = $id_save;
            $title = $title_save;
            $description = $description_save;
            $poster = $poster_save;
            $status = $status_save;
            $action = $_POST['Action'];
        }
    } else if (!empty($_POST['Action']) && $_POST['Action'] === "Delete") {
        $id_delete = 0;
        if (!empty($_POST['hidd_delete_id'])) {
            $id_delete = $_POST['hidd_delete_id'];
        }
        api_movies::delete_movie($id_delete);
    } else if (!empty($_POST['Action']) && $_POST['Action'] === "Add All") {
        $number = 0;
        $info = "";
        while (!empty($_POST['index'.$number]) && $_POST['index'.$number] == 1) {
            $info .= add($number);
            $number++;
        }
    }
}


?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <div class="ui page grid">
            <div class="column ts-wrapper">
                <?php
                require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php';
                ?>
                <div class="ts-inner-wrapper">
                    <h1>Movies</h1>
                            <?php if (!empty($error_show)) { ?>
                                    <div class="ui error message">
                                        <i class="close icon"></i>
                                        <div class="header">
                                            Please correct these wrongs.
                                        </div>
                                        <ul class="list">
                                            <?php echo $error_show; ?>
                                        </ul>
                                    </div>
                                <?php } else if (!empty($success_info)) {
                                    ?>
                                <div class="ui success message">
                                        <i class="close icon"></i>
                                        <div class="header">
                                            <?php echo $success_info; ?>
                                        </div>
                                </div>
                                    <?php
                                } else if (!empty($info)) {
                                    echo $info;
                                }
                                ?>
                    <div class="hide" id="add-edit-popup">
                        <form action="movies.php" method="POST" id="form_delete" >
                            <input type="hidden" name="hidd_delete_id" value="0" />
                            <input type="hidden" name="Action" value="Delete" />
                        </form>
                        <form name="form_data" id="form_data" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data">
                        <div class="ui form segment form-background">
                            
                            <input type="hidden" name="Id" value="<?php echo $id; ?>" />
                            
                            <div class="field">
                                <label for="Title">Title <span class="required">*</span></label>
                                <input id="Title" name="Title" value="<?php echo $title ?>" placeholder="Title" type="text">
                            </div>

                            <div class="field">
                                <label for="Status">Status</label>
                                <div class="ui selection dropdown">
                                    <input type="hidden" name="Status" value="<?php echo $status; ?>">
                                    <?php 
                                    
                                    ?>
                                    <div class="text"><?php echo $status; ?></div>
                                    <i class="dropdown icon"></i>
                                    <div class="menu ui transition hidden">
                                      <?php 
                                      
                                        $list = api_movies::get_all_movie_statuses();
                                        $text = "";
                                        if (!empty($list) && count($list) > 0) {
                                            foreach ($list as $item) {?>
                                                <div class="item"><?php echo $item['name']; ?></div>
                                      <?php
                                            } 
                                        }?>
                                    </div>
                                  </div>
                            </div>

                            <div class="field">
                                <label for="Description">Description <span class="required">*</span></label>
                                <textarea id="Description" name="Description" placeholder="Description" class="form-control" rows="3"><?php echo $description; ?></textarea>
                            </div>

                            <div class="field">
                                <label for="Poster">Poster <span class="required">*</span></label>
                                
                                <input type="file" name="File_Poster" />
                                <?php 
                                if (!empty($poster)) { ?>
                                    <input name="Poster" type="hidden" value="<?php echo $poster; ?>">
                                    <img src="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_IMAGES_POSTERS . $poster; ?>" style="max-height: 100px;">
                                <?php 
                                } ?>
                            </div>
                            
                            
                            
                            <input type="hidden" name="Action" value="add">
                            <div class="ui buttons">
                                <a href="?"><div class="ui  button">Cancel</div></a>
                                <div class="or"></div>
                                <div id="action-button" class="ui orange button" onclick="submit_data();"><?php echo $action; ?></div>
                            </div>

                        </div>
                        </form>
                    </div>
                    
                    <div id="adds">
                        
                    </div>
                    
                    <h2>List</h2>
                    
                    <table class="ui table segment">
                        <thead>
                          <tr><th>Title</th>
                          <th>Status</th>
                          <th>Description</th>
                          <th>Poster</th>
                          <th>Actions</th>
                        </tr></thead>
                        <tbody>
                            <?php 
                            $list = api_movies::get_all_movies();
                            if (empty($list) || count($list) == 0) {
                                echo "<tr><td>No item found.</td></tr>";
                            } else {
                                $current_page = 1;
                                if (!empty($_GET['p'])) {
                                        $current_page = $_GET['p'];
                                }
                                
                                $list_total = $list;
                                $page_size = 10;
                                $lib_pager = new lib_pager($list_total, $page_size);
                                $total_page = $lib_pager->get_total_page();
                                $current_page_list = $lib_pager->get_current_page_list($current_page);

                                foreach ($current_page_list as $item) {?>

                          <tr>
                            <td><?php echo $item['title'];?></td>
                            <td><?php echo $item['status_name'];?></td>
                            <td><?php echo substr(strip_tags($item['description']), 0, 200); if (strlen($item['description']) > 195) {echo "...[More]";}?></td>
                            <td>
                                <a href="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_IMAGES_POSTERS . $item['poster']; ?>" target="_blank">
                                    <img src="<?php echo LINK_ROOT . DIR_SHARED_UPLOAD_IMAGES_POSTERS . $item['poster']; ?>" style="height:80px;width:60px;" />
                                </a>
                            </td>
                            <td>
                                <a href="?Action=Edit&Id=<?php echo $item['id']; ?>">
                                <i class="pencil icon"></i>
                                </a>
                                <i class="remove icon" onclick="confirmDelete('<?php echo $item['id']; ?>')"></i>
                            </td>
                          </tr>
                            <?php 
                                } 
                            }?>
                        </tbody>
                        <tfoot>
                          <tr><th colspan="5">
                            <label>Entry number </label>
                            <div class="ui input">
                                <input id="txt_entry_number" name="txt_entry_number" type="text" width="3em" placeholder="1">
                            </div>
                            <div class="ui orange labeled icon button" onclick="show_add_popup('add_popup_movies');" ><i class="video icon"></i> Add Movies</div>
                            <table class="paging-footer">
                                    <tbody>
                                            <tr>
                                                    <?php 
                                                    if (!empty($total_page) && count($total_page) > 0) {
                                                        for ($page = 1; $page <= $total_page; $page++) { ?>
                                                        <td>
                                                                <span>
                                                                        <a href="?p=<?php echo $page; ?>" <?php if ($current_page != $page) echo 'style="color:Black;"'; ?>><?php echo $page; ?></a>
                                                                </span>
                                                        </td>
                                                    <?php }
                                                    }?>
                                            </tr>
                                    </tbody>
                            </table>
                          </th>
                        </tr></tfoot>
                      </table>
                    
                    <script type="text/javascript">
                        <?php 
                        if (!empty($_GET['Action']) && $_GET['Action'] === "Edit") {
                            echo "show_popup();";
                        }
                        ?>
                    </script>
                </div>
                <?php
                require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php';
                ?>
            </div>
        </div>
    </body>
</html>